<template>
  <div>
    <div style="overflow: hidden">
      <LianxiOne
        v-for="(item, index) in list"
        :key="index"
        :obj="item"
        @select="fn"
      ></LianxiOne>
    </div>
    <div>
      <h3>你喜欢的狗：</h3>
      <ul>
        <li v-for="(item, index) in likeList" :key="index">{{ item }}</li>
      </ul>
    </div>

    <hr />

    <div>
      <LianxiTwo
        v-for="(item, index) in goodsArr"
        :key="index"
        :obj="item"
        @add="item.count = $event - 0"
      ></LianxiTwo>
      <p>All Number:{{ total }}</p>
    </div>

    <hr />

    <div>
      <div>
        <h2>商品清单如下:</h2>
        <ul>
          <li v-for="(item, index) in foodList" :key="index">
            {{ item.shopName }} {{ item.price }} 元/份
          </li>
        </ul>
      </div>

      <div>
        <LianxiThree
          v-for="(item, index) in foodList"
          :key="index"
          :obj="item"
          @changenum-event="item.count = $event"
        ></LianxiThree>
      </div>

      <p>总价为：{{allPrice}}</p>
    </div>

    <hr>

    <div>
       <table
      border="1"
      width="700"
      style="border-collapse: collapse"
    >
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th>
            <input type="checkbox" v-model="all"/> <span>全选</span>
          </th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <HomeWork v-for="(item, index) in goodList" 
        :key="index"
        :obj="item"
        :index="index"
        @controlnum-event="item.num = $event"
        @deltr-event="del"
        @change-all-event="changeChecked"></HomeWork>
      </tbody>
      <tfoot>
        <tr>
          <td>合计:</td>
          <td colspan="5">
            {{totalprice}}
          </td>
        </tr>
      </tfoot>
    </table>
    </div>

    <hr>

    <div>
      <HomeworkOne v-for="(item, index) in arr"
      :key="index"
      :obj="item"
      :index="index"
      @tijiao="item.result = $event"></HomeworkOne>
    </div>
    <div>
      <MySubject v-for="(item, index) in arr"
      :key="index"
      :obj="item"
      :index="index"></MySubject>
    </div>
  </div>
</template>

<script>
import LianxiOne from "@/components/LianxiOne.vue";
import LianxiTwo from "@/components/LianxiTwo.vue";
import LianxiThree from "@/components/LianxiThree.vue";
import HomeWork from "@/components/HomeWork.vue";
import HomeworkOne from "@/components/HomeworkOne.vue"
import MySubject from "@/components/MySubject.vue"
export default {
  components: {
    LianxiOne,
    LianxiTwo,
    LianxiThree,
    HomeWork,
    HomeworkOne,
    MySubject
  },
  props: {},
  data() {
    return {
      likeList: [],
      arr:[],
      list: [
        {
          dogImgUrl:
            "http://nwzimg.wezhan.cn/contents/sitefiles2029/10146688/images/21129958.jpg",
          dogName: "博美",
        },
        {
          dogImgUrl:
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1224576619,1307855467&fm=26&gp=0.jpg",
          dogName: "泰迪",
        },
        {
          dogImgUrl:
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2967740259,1518632757&fm=26&gp=0.jpg",
          dogName: "金毛",
        },
        {
          dogImgUrl:
            "https://pic1.zhimg.com/80/v2-7ba4342e6fedb9c5f3726eb0888867da_1440w.jpg?source=1940ef5c",
          dogName: "哈士奇",
        },
        {
          dogImgUrl:
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563813435580&di=946902d419c3643e33a0c9113fc8d780&imgtype=0&src=http%3A%2F%2Fvpic.video.qq.com%2F3388556%2Fd0522aynh3x_ori_3.jpg",
          dogName: "阿拉斯加",
        },
        {
          dogImgUrl:
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563813454815&di=ecdd2ebf479568453d704dffacdfa12c&imgtype=0&src=http%3A%2F%2Fwww.officedoyen.com%2Fuploads%2Fallimg%2F150408%2F1-15040Q10J5B0.jpg",
          dogName: "萨摩耶",
        },
      ],
      goodsArr: [
        {
          count: 0,
          goodsName: "Watermelon",
        },
        {
          count: 0,
          goodsName: "Banana",
        },
        {
          count: 0,
          goodsName: "Orange",
        },
        {
          count: 0,
          goodsName: "Pineapple",
        },
        {
          count: 0,
          goodsName: "Strawberry",
        },
      ],
      foodList: [
        {
          shopName: "可比克薯片",
          price: 5.5,
          count: 0,
        },
        {
          shopName: "草莓酱",
          price: 3.5,
          count: 0,
        },
        {
          shopName: "红烧肉",
          price: 55,
          count: 0,
        },
        {
          shopName: "方便面",
          price: 12,
          count: 0,
        },
      ],
      goodList: [
        {
          name: "诸葛亮",
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          name: "蔡文姬",
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          name: "妲己",
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          name: "鲁班",
          price: 2200,
          num: 1,
          checked: false,
        },
      ],
    };
  },
  computed: {
    total() {
      return this.goodsArr.reduce((sum, item) => (sum += item.count), 0);
    },
    allPrice(){

      return this.foodList.reduce((sum, item) => sum += item.count * item.price, 0);
    },
    totalprice(){
      return this.goodList.reduce((sum, item) => sum += item.num * item.price, 0)
    },

    all: {
      get() {
        return this.goodList.every((item) => item.checked === true);
      },
      set(value) {
        return this.goodList.forEach((item) => (item.checked = value));
      },
    },
  },
  watch: {},
  created() {
    const arr = [];
    for (let i = 0; i < 5; i++){
      const left = Math.floor(Math.random() * 11)
      const right = Math.floor(Math.random() * 11)
      arr.push({left, right, result: null});
    }
    this.arr = arr;
  },
  methods: {
    fn(dogName) {
      this.likeList.push(dogName);
      console.log(this.likeList);
    },
    del(index){
      this.goodList.splice(index, 1)
    },
    changeChecked(index) {
      this.goodList[index].checked = !this.goodList[index].checked
      },
  },
};
</script>

<style scoped></style>
